package io.framework.modules.soil.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.modules.soil.dao.SoilDao;
import io.framework.modules.soil.dto.SoilDTO;
import io.framework.modules.soil.entity.SoilEntity;
import io.framework.modules.soil.service.SoilService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 土壤环境质量监测
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-25
 */
@Service
public class SoilServiceImpl extends CrudServiceImpl<SoilDao, SoilEntity, SoilDTO> implements SoilService {

    @Override
    public QueryWrapper<SoilEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<SoilEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public PageData<SoilDTO> page(Map<String, Object> params) {

        IPage<SoilEntity> pages = getPage(params, Constant.CREATE_DATE, false);

        List<SoilEntity> list = baseDao.getList(params);

        return getPageData(list, pages.getTotal(), SoilDTO.class);
    }

    @Override
    public PageData<SoilDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String villageName = jsonObject.getString("villageName");
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<SoilEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(villageName)) {
            queryWrapper.like("village_name", villageName);
        }
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(month,'%Y')", time);
        }
        Page<SoilEntity> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<SoilEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<SoilDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), SoilDTO.class);
        return new PageData<SoilDTO>(dtoList, ip.getTotal());
    }

    @Override
    public List<Map<String, Object>> getPassInfo(Map<String, Object> params) {

        return baseDao.getPassInfo(params);
    }

    @Override
    public List<Map<String, Object>> getPassNum(Map<String, Object> params) {

        return baseDao.getPassNum(params);
    }

    @Override
    public List<Map<String, Object>> getMonitorPointInfo(Map<String, Object> params) {

        return baseDao.getMonitorPointInfo(params);
    }

    @Override
    public List<Map<String, Object>> getHistoryQuality(Map<String, Object> params) {
        return baseDao.getHistoryQuality(params);
    }

    @Override
    public List<Map<String, Object>> getVillageNameList() {

        return baseDao.getVillageNameList();
    }

    @Override
    public List<Map<String, Object>> getPointNamaList(Map<String, Object> params) {

        return baseDao.getPointNamaList(params);
    }

    @Override
    public List<SoilDTO> getSoilStationInfoById(Map<String, Object> params) {

        return baseDao.getSoilStationInfoById(params);
    }

    @Override
    public Boolean checkData(String year) {
        Boolean flag = false;
        List<SoilDTO> list = baseDao.checkData(year);
        if (CollectionUtils.isNotEmpty(list)) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Map<String, Object>> getPassNumSort(Map<String, Object> params) {

        return baseDao.getPassNumSort(params);
    }

    @Override
    public void deleteInfoByDate(String year) {
        Map<String, Object> params = new HashMap<>(1);
        params.put("year", year);
        baseDao.deleteInfoByDate(params);
    }

    @Override
    public String  getDate() {
       return  baseDao.getDate();
    }

}
